package com.hundred.item.manager.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hundred.item.dao.ItemDao;
import com.hundred.item.dao.dataobject.ItemDO;
import com.hundred.item.dao.dataobject.ItemReqDO;
import com.hundred.item.manager.ItemManager;

public class ItemManagerImpl implements ItemManager {

	private static final Logger log = LoggerFactory.getLogger(ItemManagerImpl.class);

	private ItemDao itemDao;

	private SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");

	public boolean addItem(ItemDO itemVo) throws Exception {return true;}

	
	public boolean deleteItem(ItemDO itemVo) throws Exception {
		if(itemDao.deleteItem(itemVo.getItemId())>0){
			return true;
		}else{
			return false;
		}
	}

	public void setItemDao(ItemDao itemDao) {
		this.itemDao = itemDao;
	}

	public void insertItem(ItemDO itemDo) throws Exception {
		
		long itemId = itemDao.insertItem(itemDo);
		if(itemId<=0){
			return;
		}
		itemDo.setItemId(itemId);
		this.updateItem(itemDo);
		
	}

	public int updateItem(ItemDO itemDo) throws Exception {
		return itemDao.updateItem(itemDo);
	}

	public int deleteItem(long itemId) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	public ItemDO getItemById(ItemReqDO itemReq) {
		ItemDO itemDO = itemDao.getItemById(itemReq);
		if(itemDO!=null){
			titleProcess(itemDO);
			priceProcess(itemDO);
			itemUrlProcess(itemDO);
		}
		return itemDO;
	}

	public long countTotalItemNumber(int status) {
		return itemDao.countTotalOnlineItems(status);
	}

	public List<ItemDO> getTopItemList(int status, long startRow, long endRow) {
		List<ItemDO> listItems =  itemDao.getTopItemsList(status, startRow, endRow);
		if(listItems!=null && listItems.size()>0){
			for(ItemDO item:listItems){
				titleProcess(item);
				priceProcess(item);
				itemUrlProcess(item);
			}
		}
		return listItems;
	}

	public ItemDO getHistoryItemById(long itemId) {
		ItemDO itemDO = itemDao.getHistoryItemById(itemId);
		if(itemDO!=null){
			titleProcess(itemDO);
			priceProcess(itemDO);
			itemUrlProcess(itemDO);
		}
		return itemDO;
	}

	private void titleProcess(ItemDO itemDO){
		
		if(itemDO!=null && itemDO.getItemName()!=null){
			String[] titleStrArray = itemDO.getItemName().split("。");
			String[] titleStrArray2 = itemDO.getItemName().split("，");
			if(titleStrArray.length>=1 && titleStrArray2.length>=1){
				String oneStr = titleStrArray[0];
				String twoStr = titleStrArray2[0];
				String newTitle = oneStr.length()<=twoStr.length()?oneStr:twoStr;
				itemDO.setItemName(newTitle);
			}
			else if(titleStrArray.length>=1){
				String newTitle = titleStrArray[0];
				itemDO.setItemName(newTitle);
			}

		}
	}
	
	private void priceProcess(ItemDO itemDO){
		if(itemDO.getItemPrice()>0){
			
			BigDecimal bg = new BigDecimal((itemDO.getItemPrice())/100.00);
	        double newprice = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
			itemDO.setPriceStr(String.valueOf(newprice));
		}
	}
	private void itemUrlProcess(ItemDO itemDO){
		
		String ourTrackId="10037056";
		
		String trackId = "&ali_trackid=2:mm_10037056_0_0:1353977147_4k2_691519382";
		
		if(itemDO!=null && itemDO.getItemUrl()!=null){
			String itemUrlTemp = itemDO.getItemUrl().replaceAll("amp;", "");
			String finalItemUrl = itemUrlTemp;
			if(itemUrlTemp.indexOf("mm_")>-1){
				int aliIndex = itemUrlTemp.indexOf("mm_");
				finalItemUrl = itemUrlTemp.substring(0, aliIndex+3)
						+ourTrackId
						+itemUrlTemp.substring(itemUrlTemp.indexOf("_",aliIndex+4));
			}
			else{
				finalItemUrl=finalItemUrl+trackId;
			}
			itemDO.setItemUrl(finalItemUrl);
			log.error("item-url:"+itemUrlTemp);
		}
		if(itemDO!=null && itemDO.getItemPic()!=null){
			log.error("URL-Prossesing----"+itemDO.getItemPic());
			String itemPicStr = itemDO.getItemPic();
			if(itemPicStr.indexOf("_310x310.jpg")>-1){
				itemPicStr=itemPicStr.replaceAll("_310x310.jpg", "");
				itemDO.setItemPic(itemPicStr);
			}
		}
	}
	
	public static void main(String args[]){
		ItemManagerImpl dd= new ItemManagerImpl();
		ItemDO item = new ItemDO();
		item.setItemUrl("http://s.click.taobao.com/t_8?e=7HZ6jHSTZPhc3yRJRF%2Fa6cZZYIwKZaWVLo%2BoWLPHtjyxLKii7iwH8IooOwVdnXSY3e93m4dqYVta2Jfat%2F7Ik%2Ftw9LJB3xNNfl2ybcSjSgdJ3ac%3D&p=mm_28514026_0_0&n=19");
		item.setItemUrl("http://item.taobao.com/item.htm?id=9892769239&ali_trackid=2:mm_28514026_0_0:1353944393_310_42855049");
		dd.itemUrlProcess(item);
		System.out.println(item.getItemUrl());
	}
	
}
